package com.silong.swing.myWindows;

import javax.swing.*;
import javax.swing.plaf.ColorUIResource;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by 龙哥 on 2022-01-17 15:38
 * Silong Software.
 * 适配器类
 */

public class MyFrameAdapter extends JFrame {

    JButton button1;
    JButton button2;

    public MyFrameAdapter(){
        init();
    }

    private void init(){

        button1 = new JButton("按钮一");
        button2 = new JButton("按钮二");

        Container window = this.getContentPane();
        this.setSize(800, 600);
        this.setLocationRelativeTo(null); //距中
        this.setTitle("自定义控件大小位置");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //退出窗口关闭程序

        //设置布局为空，优点:摆放灵活，缺点：代码量大，自适应能力差。
        this.setLayout(null);

        //自定义按钮的位置 ，大小
        button1.setLocation(100,50); //设置按钮位置
        button1.setSize(200,50); //设置按钮大小
        button2.setBounds(400, 50, 200, 50); //一次性设置位置与大小

        //添加鼠标适配器类进行事件监听
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                //鼠标按钮为蓝色
                button2.setBackground(new Color(0,0,255));
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                //鼠标移走时为紫色
                //button2.setBackground(new Color(255,0,255));
                button2.setBackground(new Color(0XFF00FF)); //用16进制表示颜色
            }
        });
        window.add(button1);
        window.add(button2);

    }
}
